package com.kly.cashmall.utils.system;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CallLog;
import android.provider.ContactsContract;

import androidx.core.content.ContextCompat;
import com.kly.cashmall.bean.ContactPostBean.CallRecordVO;
import com.kly.cashmall.bean.ContactPostBean.ContactVO;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zxp
 * @since 27/10/21
 */
public class ContactService {

    private static ContactService contactService;

    public ContactService() {
    }

    public static ContactService getInstance() {
        contactService = new ContactService();
        return contactService;
    }

    public List<ContactVO> getContactStr(Context context) {
        if (context == null) {
            return Collections.emptyList();
        } else {
            List<ContactVO> contactVOs = new ArrayList();
            if (Build.VERSION.SDK_INT >= 23) {
                ContentResolver cr = context.getContentResolver();
                String[] str = new String[]{"display_name", "data1", "last_time_contacted", "times_contacted"};
                Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, str, (String)null, (String[])null, (String)null);
                if (cur != null && cur.getCount() > 0 && cur.moveToFirst()) {
                    while(cur.moveToNext()) {
                        ContactVO contactVO = new ContactVO(cur.getString(cur.getColumnIndex("display_name")), cur.getString(cur.getColumnIndex("data1")), cur.getString(cur.getColumnIndex("last_time_contacted")), cur.getString(cur.getColumnIndex("times_contacted")));
                        contactVOs.add(contactVO);
                    }

                    cur.close();
                }
            } else {
                String[] cols = new String[]{"display_name", "data1", "last_time_contacted", "times_contacted"};
                Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, cols, (String)null, (String[])null, (String)null);

                for(int i = 0; i < cursor.getCount(); ++i) {
                    cursor.moveToPosition(i);
                    int nameFieldColumnIndex = cursor.getColumnIndex("display_name");
                    int numberFieldColumnIndex = cursor.getColumnIndex("data1");
                    int lastTimeFieldColumnIndex = cursor.getColumnIndex("last_time_contacted");
                    int timesFieldColumnIndex = cursor.getColumnIndex("times_contacted");
                    String name = cursor.getString(nameFieldColumnIndex);
                    String number = cursor.getString(numberFieldColumnIndex);
                    String lasttime = cursor.getString(lastTimeFieldColumnIndex);
                    String times = cursor.getString(timesFieldColumnIndex);
                    ContactVO contactVO = new ContactVO(name, number, lasttime, times);
                    contactVOs.add(contactVO);
                }
            }

            return contactVOs;
        }
    }

    public List<CallRecordVO> getCallStr(Context context) {
        ContentResolver cr = context.getContentResolver();
        String[] projection = new String[]{"date", "number", "type", "name", "duration"};
        Uri uri = CallLog.Calls.CONTENT_URI;

        Cursor cursor;
        try {
            if (ContextCompat.checkSelfPermission(context, "android.permission.READ_CALL_LOG") != 0) {
            }

            cursor = cr.query(uri, projection, (String)null, (String[])null, "date DESC");
        } catch (SecurityException var15) {
            var15.printStackTrace();
            return null;
        }

        List<CallRecordVO> callRecordVOs = new ArrayList();
        List<CallRecordVO> callRecordVOsScreening = new ArrayList();
        if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
            while(cursor.moveToNext()) {
                String type = cursor.getString(cursor.getColumnIndex("type"));
                switch(Integer.parseInt(type)) {
                    case 1:
                        type = "呼入";
                        break;
                    case 2:
                        type = "呼出";
                        break;
                    case 3:
                        type = "未接";
                        break;
                    default:
                        type = "挂断";
                }

                CallRecordVO callRecordVO = new CallRecordVO(cursor.getString(cursor.getColumnIndex("name")), cursor.getString(cursor.getColumnIndex("number")), type, cursor.getString(cursor.getColumnIndex("date")), cursor.getString(cursor.getColumnIndex("duration")));
                callRecordVOs.add(callRecordVO);
            }

            cursor.close();
        }

        long timeMillis = System.currentTimeMillis();
        long lastTimeMillis = timeMillis - 7948800000L;

        for(int i = 0; i < callRecordVOs.size(); ++i) {
            long time = Long.parseLong(((CallRecordVO)callRecordVOs.get(i)).getCallTime());
            if (time > lastTimeMillis) {
                callRecordVOsScreening.add(callRecordVOs.get(i));
            }
        }

        return callRecordVOsScreening;
    }

    public List<CallRecordVO> getCallStr(Context context, boolean change) {
        ContentResolver cr = context.getContentResolver();
        String[] projection = new String[]{"date", "number", "type", "name", "duration"};
        Uri uri = CallLog.Calls.CONTENT_URI;

        Cursor cursor;
        try {
            if (ContextCompat.checkSelfPermission(context, "android.permission.READ_CALL_LOG") != 0) {
            }

            cursor = cr.query(uri, projection, (String)null, (String[])null, (String)null);
        } catch (SecurityException var10) {
            var10.printStackTrace();
            return null;
        }

        List<CallRecordVO> callRecordVOs = new ArrayList();
        if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
            while(cursor.moveToNext()) {
                String type = cursor.getString(cursor.getColumnIndex("type"));
                CallRecordVO callRecordVO = new CallRecordVO(cursor.getString(cursor.getColumnIndex("name")), cursor.getString(cursor.getColumnIndex("number")), type, cursor.getString(cursor.getColumnIndex("date")), cursor.getString(cursor.getColumnIndex("duration")));
                callRecordVOs.add(callRecordVO);
            }

            cursor.close();
        }

        return callRecordVOs;
    }
}
